/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package air.pagehandler;

import air.eb.Flightinstance;
import air.eb.Seatinstance;
import air.sb.AirRemote;
import air.sb.LuftRemote;
import air.sb.SASRemote;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.*;

/**
 *
 * @author mrxyz
 */
public class BookTripBean {

    AirRemote AB = lookupAirBean();
    LuftRemote LB = lookupLuftBean();
    SASRemote SB = lookupSASBean();

    public Collection<Seatinstance> getFreeSeats() {
        Collection<Seatinstance> res;
        if ((res = AB.getFreeSeats()) == null) {
            if ((res = LB.getFreeSeats()) == null) {
                res = SB.getFreeSeats();
            }
        }
        return res;
    }

    public Collection<Flightinstance> getFlights() {
        Collection<Flightinstance> res;
        if ((res = AB.getRoute()) == null) {
            if ((res = LB.getRoute()) == null) {
                res = SB.getRoute();
            }
        }
        return res;
    }

    public int getNumberOfPassengers() {
        int res;
        if ((res = AB.getNumberOfPassengers()) == 0) {
            if ((res = LB.getNumberOfPassengers()) == 0) {
                res = SB.getNumberOfPassengers();
            }
        }
        return res;
    }

    private AirRemote lookupAirBean() {
        try {
            Context c = new InitialContext();
            return (AirRemote) c.lookup("java:comp/env/AirBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private LuftRemote lookupLuftBean() {
        try {
            Context c = new InitialContext();
            return (LuftRemote) c.lookup("java:comp/env/LuftBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private SASRemote lookupSASBean() {
        try {
            Context c = new InitialContext();
            return (SASRemote) c.lookup("java:comp/env/SASBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
